<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateProductPrice{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validatePrice = new Zend_Validate();
        $validateDiscount = new Zend_Validate();
        
        $validatePrice->addValidator(new Zend_Validate_Digits(), true);
        $validateDiscount->addValidator(new Zend_Validate_Digits(), true);
        
        /*
        * Check Price
        */
        
        foreach($data as $row){
            if(!$validatePrice->isValid($params['price'][$row['id']])){
                $this->_messageError[$row['id']]['price'] = '<label class="control-label" for="inputError">' . $translate->_('price') . current($validatePrice->getMessages()) . '</label>';
                $this->_arrData[$row['id']]['price'] = 0;
                $this->_classError[$row['id']] = 'has-error';
                $this->_isError = true;
            }else{
                $this->_messageError[$row['id']]['price'] = '';
                $this->_arrData[$row['id']]['price'] = $params['price'][$row['id']];
                $this->_classError[$row['id']] = '';
            }
            
            if(!$validateDiscount->isValid($params['discount'][$row['id']])){
                $this->_messageError[$row['id']]['discount'] = '<label class="control-label" for="inputError">' . $translate->_('discount name') . current($validateDiscount->getMessages()) . '</label>';
                $this->_arrData[$row['id']]['discount'] = 0;
                $this->_classError[$row['id']] = 'has-error';
                $this->_isError = true;
            }else{
                $this->_messageError[$row['id']]['discount'] = '';
                $this->_arrData[$row['id']]['discount'] = $params['discount'][$row['id']];
                
                if(!$this->_isError){
                    $this->_classError[$row['id']] = '';
                }
            }
            
            $discount_type = false;
            
            if($params['discount_type'][$row['id']] == 1){
                $discount_type = true;
            }
            
            $this->_arrData[$row['id']]['discount_type'] = $discount_type;
            $this->_arrData[$row['id']]['name'] = $row['name'];
            $this->_arrData[$row['id']]['p_price'] = $row['p_price'];
            $this->_arrData[$row['id']]['id'] = $row['id'];
            $this->_arrData[$row['id']]['brand'] = $row['brand'];
            $this->_arrData[$row['id']]['net'] = $row['net'];
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}